home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / c / qtools0.2-src.lha / src / libqtools / script.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-07-18  |  26.9 KB  |  770 lines

  1. #define    LIBQTOOLS_CORE
  2. #include "../include/libqtools.h"
  3. #include "../include/libqbuild.h"
  4. #include "../include/libqdisplay.h"
  5.  
  6. /*
  7.  * Script-System:
  8.  * 
  9.  * commands:
  10.  * "process <file>.<ext>"                               -> take this file upto
  11.  * "finish <file>.<ext>"                                -> here
  12.  * "list <file>.<ext>"                                  -> list the contents
  13.  * "view <file>.<ext>"                                  -> view the pictures or texts in a window, or whatever
  14.  * 
  15.  * sub-commands <PAK>:
  16.  * "OP_UPDATE <file>.<ext> as <name>"                   -> OP_ADD if not exists, OP_REPLACE if newer
  17.  * "OP_REPLACE <file>.<ext> as <name>"                  -> OP_REPLACE if exists, do NOT OP_ADD
  18.  * "OP_ADD <file>.<ext> as <name>"                      -> OP_ADD if not exists, do NOT OP_REPLACE
  19.  * "OP_DELETE <name>"                                   -> OP_DELETE if exists
  20.  * "OP_EXTRACT <name> as <file>.<ext>"                  -> OP_EXTRACT if exists
  21.  * 
  22.  * sub-commands <WAD>:
  23.  * -multiple same names are allowed, but with different types
  24.  * "OP_UPDATE <file>.<ext> as <name> as <type>"         -> OP_ADD if not exists, OP_REPLACE if newer
  25.  * "OP_REPLACE <file>.<ext> as <name> as <type>"        -> OP_REPLACE if exists, do NOT OP_ADD
  26.  * "OP_ADD <file>.<ext> as <name> as <type>"            -> OP_ADD if not exists, do NOT OP_REPLACE
  27.  * "OP_DELETE <name> as <type>"                         -> OP_DELETE if exists
  28.  * "OP_EXTRACT <name> as <file>.<ext> as <type>"        -> OP_EXTRACT if exists
  29.  * 
  30.  * sub-commands <BSP>:
  31.  * sub-commands <MDL>:
  32.  * 
  33.  */
  34.  
  35. /*
  36.  * basic main-skeleton
  37.  *
  38.  * extension-type system:
  39.  *  .wad -> wad-archive
  40.  *  .bsp -> bsp
  41.  *  .pak -> pak-archive
  42.  *  .mdl -> models
  43.  *  .spr -> sprites
  44.  *
  45.  *  .ppm -> ppm-picture
  46.  *  .mip -> miptexture
  47.  *  .lmp -> console-picture
  48.  *  .stb -> statusbar-picture
  49.  *  .pal -> palettes
  50.  *  .wav -> wave-sounds
  51.  *  .dat -> pseudo-code
  52.  *  .rc  -> resource
  53.  *  .cfg -> config
  54.  *  .tri -> triangle
  55.  *  .map -> map
  56.  *  .skn -> skin-picture (lmp)
  57.  *  .frm -> frame-picture (lmp)
  58.  *
  59.  * hierachy-system:
  60.  *   game                                               -> pak?.pak
  61.  *   game/pak?.dir                                      -> ?.dat, ...
  62.  *   game/pak?.dir/gfx                                  -> ?.lmp
  63.  *   game/pak?.dir/maps                                 -> ?.bsp
  64.  *   game/pak?.dir/maps/?.dir                           -> ?.map, ?.wad
  65.  *   game/pak?.dir/maps/?.dir/?.dir                     -> ?.mip/ppm, ?.lmp, ?.pal, ?.stb
  66.  *   game/pak?.dir/progs                                -> ?.mdl, ?.spr
  67.  *   game/pak?.dir/progs/?.dir                          -> ?-?.tri, ?-?.skn
  68.  *   game/pak?.dir/progs/?.dir                          -> ?-?.frm
  69.  *   game/pak?.dir/sounds                               -> ?.wav
  70.  * example:
  71.  *   game/pak0.pak
  72.  *   game/pak0.dir/default.cfg                          -> pak0.pak  (makpak)
  73.  *   game/pak0.dir/gfx/colormap.lmp                     -> pak0.pak  (makpak)
  74.  *   game/pak0.dir/gfx/conback.lmp                      -> pak0.pak  (repak)
  75.  *   game/pak0.dir/gfx/palette.lmp                      -> pak0.pak  (repak)
  76.  *   game/pak0.dir/maps/start.bsp                       -> pak0.pak  (repak)
  77.  *   game/pak0.dir/maps/start.dir/start.map             -> start.bsp (qbsp)
  78.  *   game/pak0.dir/maps/start.dir/start.wad             -> start.bsp (qbsp)
  79.  *   game/pak0.dir/maps/start.dir/start.dir/text1.mip   -> start.wad (rewad)
  80.  *   game/pak0.dir/maps/start.dir/start.dir/text2.mip   -> start.wad (rewad)
  81.  *   game/pak0.dir/progs/ogre.mdl                       -> pak0.pak  (repak)
  82.  *   game/pak0.dir/progs/ogre.dir/ogre-0.tri            -> ogre.mdl  (mdlgen)
  83.  *   game/pak0.dir/progs/ogre.dir/ogre-1.tri            -> ogre.mdl  (mdlgen)
  84.  *   game/pak0.dir/progs/ogre.dir/ogre-0.skn            -> ogre.mdl  (mdlgen)
  85.  *   game/pak0.dir/progs/fire.spr                       -> pak0.pak  (repak)
  86.  *   game/pak0.dir/progs/fire.dir/fire-0.frm            -> fire.spr  (sprgen)
  87.  *   game/pak0.dir/progs/fire.dir/fire-1.frm            -> fire.spr  (sprgen)
  88.  */
  89.  
  90. char *OperationToText(operation Oper)
  91. {
  92.   switch(Oper) {
  93.     case OP_ADD:    return "add";
  94.     case OP_DELETE:    return "delete";
  95.     case OP_EXTRACT:    return "extract";
  96.     case OP_LIST:    return "list";
  97.     case OP_REPLACE:    return "replace";
  98.     case OP_UPDATE:    return "update";
  99.     case OP_VIEW:    return "view";
  100.     case OP_DEFAULT:
  101.     default:        return "default action";
  102.   }
  103. }
  104.  
  105. void AppendType(char *Original, filetype Type, char *Default) {
  106.   switch (Type) {
  107.     case TYPE_PPM:      __strncat(Original, ".ppm", NAMELEN_PATH - 1);  break;
  108.     case TYPE_JPEG:     __strncat(Original, ".jpg", NAMELEN_PATH - 1);  break;
  109.     case TYPE_ILBM:     __strncat(Original, ".iff", NAMELEN_PATH - 1);  break;
  110.     case TYPE_PNG:      __strncat(Original, ".png", NAMELEN_PATH - 1);  break;
  111.     case TYPE_WAD2:     __strncat(Original, ".wad", NAMELEN_PATH - 1);  break;
  112.     case WAD2_PALETTE:   __strncat(Original, ".pal", NAMELEN_PATH - 1);  break;
  113.     case WAD2_STATUSBAR: __strncat(Original, ".stb", NAMELEN_PATH - 1);  break;
  114.     case WAD2_MIPMAP:    __strncat(Original, ".mip", NAMELEN_PATH - 1);  break;
  115.     case WAD2_CONPIC:    __strncat(Original, ".lmp", NAMELEN_PATH - 1);  break;
  116.     case TYPE_NONE:
  117.     default:         __strncat(Original, Default, NAMELEN_PATH - 1); break;
  118.   }
  119. }
  120.  
  121. bool processType(char *procName, filetype procType,
  122.          char *destDir,
  123.          char *outName, filetype outType,
  124.          char *arcName, filetype arcType,
  125.          operation procOper,
  126.          bool script, bool recurse)
  127. {
  128.   FILE *destFile, *scrpFile, *procFile;
  129.   HANDLE destHandle;
  130.  
  131. #ifdef    PRINTCALLS
  132.   mprintf("processType(%s, %d, %s, %s, %d, %s, %d, %d, %d, %d)\n", procName, procType, destDir, outName, outType, arcName, arcType, procOper, script, recurse);
  133. #endif
  134.  
  135.   if ((procFile = __fopen(procName, F_READWRITE_BINARY_OLD))) {
  136.     /* get extension */
  137.       struct palpic *inPic;
  138.       struct rawdata *inData;
  139.       char idxName[NAMELEN_PATH + 1];
  140.  
  141.       inPic = 0;
  142.       inData = 0;
  143.  
  144.       __strncpy(idxName, destDir, NAMELEN_PATH - 1);
  145.       __strncat(idxName, procName, NAMELEN_PATH - 1);
  146.       ReplaceExt(idxName, "idx");
  147.  
  148.       /* script-based execution */
  149.       if (procType == TYPE_INDEX) {
  150.     char command[NAMELEN_PATH], parameter[NAMELEN_PATH];
  151.     char *nameStack[NAMELEN_PATH];
  152.     short int nameCount;
  153.     char *thisName;
  154.  
  155.     nameCount = 0;
  156.     nameStack[0] = 0;
  157.  
  158.     /*
  159.      * process are now stackable -> proces ... process ... finish ... finish
  160.      */
  161.     while (fscanf(procFile, "%256s %256[^\n]\n", command, parameter) != EOF) {
  162.       if (!__strcmp(command, "process")) {
  163.         if (!(thisName = nameStack[++nameCount] = smalloc(parameter)))
  164.           eprintf("cannot allocate process-stack");
  165.       }
  166.       else if ((!__strcmp(command, "dither")) && (thisName)) {
  167.         char Bool;
  168.         int Value;
  169.  
  170.         sscanf(parameter, "%c with %d", &Bool, &Value);
  171.         if (Bool == 1) {
  172.           /*
  173.            * activate dithering with specified value 
  174.            */
  175.           dither = TRUE;
  176.           dithervalue = Value;
  177.         }
  178.         else if (Bool == 0) {
  179.           /*
  180.            * deactivate dithering and reset dithervalue 
  181.            */
  182.           dither = FALSE;
  183.           dithervalue = 16;
  184.         }
  185.       }
  186.       else if ((!__strcmp(command, "compress")) && (thisName)) {
  187.         char Bool;
  188.         int Value;
  189.  
  190.         sscanf(parameter, "%c with %d", &Bool, &Value);
  191.         if (Bool == 1)
  192.           Compression = Value;
  193.         else if (Bool == 0)
  194.           Compression = CMP_NONE;
  195.       }
  196.       else if ((!__strcmp(command, OperationToText(OP_UPDATE))) && (thisName)) {
  197.         char fileName[NAMELEN_PATH], entryName[NAMELEN_MAXQUAKE], entryType;
  198.  
  199.         sscanf(parameter, "%256s as %56s as %c", fileName, entryName, &entryType);
  200.         processName(fileName, destDir, thisName, outType, entryName, entryType, OP_UPDATE, script, recurse);
  201.       }
  202.       else if ((!__strcmp(command, OperationToText(OP_REPLACE))) && (thisName)) {
  203.         char fileName[NAMELEN_PATH], entryName[NAMELEN_MAXQUAKE], entryType;
  204.  
  205.         sscanf(parameter, "%256s as %56s as %c", fileName, entryName, &entryType);
  206.         processName(fileName, destDir, thisName, outType, entryName, entryType, OP_REPLACE, script, recurse);
  207.       }
  208.       else if ((!__strcmp(command, OperationToText(OP_ADD))) && (thisName)) {
  209.         char fileName[NAMELEN_PATH], entryName[NAMELEN_MAXQUAKE], entryType;
  210.  
  211.         sscanf(parameter, "%256s as %56s as %c", fileName, entryName, &entryType);
  212.         processName(fileName, destDir, thisName, outType, entryName, entryType, OP_ADD, script, recurse);
  213.       }
  214.       else if ((!__strcmp(command, OperationToText(OP_DELETE))) && (thisName)) {
  215.         char entryName[NAMELEN_MAXQUAKE], entryType;
  216.  
  217.         sscanf(parameter, "%56s as %c", entryName, &entryType);
  218.         processName(thisName, 0, 0, outType, entryName, entryType, OP_DELETE, script, recurse);
  219.       }
  220.       else if ((!__strcmp(command, OperationToText(OP_EXTRACT))) && (thisName)) {
  221.         char fileName[NAMELEN_PATH], entryName[NAMELEN_MAXQUAKE], entryType;
  222.  
  223.         sscanf(parameter, "%256s as %56s as %c", entryName, fileName, &entryType);
  224.         processName(thisName, destDir, fileName, outType, entryName, entryType, OP_EXTRACT, script, recurse);
  225.       }
  226.       else if ((!__strcmp(command, "finish")) && (thisName)) {
  227.         tfree(nameStack[nameCount]);
  228.         thisName = nameStack[--nameCount];
  229.       }
  230.       else
  231.         eprintf("unknown command, cannot execute line \"%s\" ... \n", command);
  232.     }
  233.     /* free resources in case of unfinished processes */
  234.     while (nameCount)
  235.       tfree(nameStack[nameCount--]);
  236.       }
  237.       /* directory-based execution */
  238.       else if (procType == TYPE_DIRECTORY) {
  239.     DIR *procDir;
  240.     struct dirent *dirEnt;
  241.     
  242.     /* reset arcType if it was the same as procType */
  243.     if(arcType == TYPE_DIRECTORY)
  244.       arcType = TYPE_NONE;
  245.  
  246.     if ((procDir = opendir(procName))) {
  247.       while ((dirEnt = readdir(procDir))) {
  248.         if (__strcmp(dirEnt->d_name, ".") && __strcmp(dirEnt->d_name, "..")) {
  249.           char dirName[NAMELEN_PATH];
  250.  
  251.           __strncpy(dirName, procName, NAMELEN_PATH - 1);
  252.           __strncat(dirName, "/", NAMELEN_PATH - 1);
  253.           __strncat(dirName, dirEnt->d_name, NAMELEN_PATH - 1);
  254.           /*
  255.            * if outName == 0, process the dir entirely
  256.            * else add the complete to outName
  257.            */
  258.           processName(dirName, destDir, outName, outType, 0, arcType, procOper, script, recurse);
  259.         }
  260.       }
  261.       closedir(procDir);
  262.     }
  263.     else
  264.       eprintf("failed to access directory %s\n", procName);
  265.       }
  266.       /*
  267.        * parts to archive or ppm 
  268.        */
  269.       else if ((procType == TYPE_PALETTE) ||
  270.            (procType == TYPE_CONFIG) ||
  271.            (procType == TYPE_DEMO) ||
  272.            (procType == TYPE_FRAME) ||
  273.            (procType == TYPE_ILBM) ||
  274.            (procType == TYPE_JPEG) ||
  275.            (procType == TYPE_LIT) ||
  276.            (procType == TYPE_LUMP) ||
  277.            (procType == TYPE_MAP) ||
  278.            (procType == TYPE_MIPMAP) ||
  279.            (procType == TYPE_PNG) ||
  280.            (procType == TYPE_PPM) ||
  281.            (procType == TYPE_RESOURCE) ||
  282.            (procType == TYPE_SKIN) ||
  283.            (procType == TYPE_STATUSBAR) ||
  284.            (procType == TYPE_TRIANGLE) ||
  285.            (procType == TYPE_VIS) ||
  286.            (procType == TYPE_WAL) ||
  287.            (procType == TYPE_WAVE)) {
  288.     if (outType == TYPE_PACK) {
  289.       mprintf("read raw %s\t-> ", procName);
  290.       inData = GetRaw(fileno(procFile), procName, 0);
  291.     }
  292.     else if (procType == TYPE_MIPMAP) {
  293.       mprintf("read mipmap %s\t-> ", procName);
  294.       inPic = GetMipMap(fileno(procFile), MIPMAP_0);
  295.     }
  296.     else if (procType == TYPE_WAL) {
  297.       mprintf("read wal %s\t-> ", procName);
  298.       inPic = 0;                        /* GetWal(procFile, 0); */
  299.     }
  300.     else if ((procType == TYPE_LUMP) || (procType == TYPE_STATUSBAR)) {
  301.       mprintf("read lump %s\t-> ", procName);
  302.       inPic = GetLMP(fileno(procFile), arcName ? arcName : procName);
  303.     }
  304.     else if (procType == TYPE_IMAGE) {
  305.       short int alignX = 1, alignY = 1;
  306.  
  307.       /* special size-rules if we convert to a mipmap */
  308.       if ((outType == TYPE_MIPMAP) || (outType == TYPE_WAD2) || (outType == TYPE_BSP)) {
  309.         alignX = alignY = 16;
  310.         if (outName[0] == WARP_MIPMAP)
  311.           alignX = alignY = WARP_X;
  312.         else if (!__strncmp(outName, SKY_MIPMAP, 3)) {
  313.           alignX = -(SKY_X);
  314.           alignY = -(SKY_Y);
  315.         }
  316.       }
  317.       mprintf("read image %s\t-> ", procName);
  318.       inPic = GetImage(procFile, arcName ? arcName : procName, alignX, alignY);
  319.     }
  320.     else if (procType == TYPE_TRIANGLE) {
  321.       mprintf("read tri %s\t-> ", procName);
  322.       inData = 0;
  323.     }
  324.     else if (procType == TYPE_IMAGINE) {
  325.       mprintf("read iob %s\t-> ", procName);
  326.       inData = GetRaw(fileno(procFile), procName, 0);
  327.     }
  328.     else if (procType == TYPE_MAP) {
  329.       mprintf("read map %s\t-> ", procName);
  330.       inData = GetRaw(fileno(procFile), procName, 0);
  331.     }
  332.     else {
  333.       mprintf("read raw %s\t-> ", procName);
  334.       inData = GetRaw(fileno(procFile), procName, 0);
  335.     }
  336.  
  337.     if (inPic || inData) {
  338.       /* convert to ppm */
  339.       if (((outType == TYPE_PPM) ||
  340.            (outType == TYPE_JPEG) ||
  341.            (outType == TYPE_ILBM) ||
  342.            (outType == TYPE_PNG)) && inPic) {
  343.         mprintf("write image %s\n", outName);
  344.         if ((destFile = __fopen(outName, F_WRITE_BINARY))) {
  345.           if (PutImage(destFile, inPic, outType) == FALSE)
  346.         eprintf("failed to convert pic\n");
  347.           __fclose(destFile);
  348.         }
  349.         else
  350.           eprintf(failed_fileopen, outName);
  351.       }
  352.       /* convert to mip */
  353.       else if ((outType == TYPE_MIPMAP) && inPic) {
  354.         mprintf("write mip %s\n", outName);
  355.         if ((destHandle = __open(outName, H_WRITE_BINARY)) > 0) {
  356.           if (PutMipMap(destHandle, inPic) == FALSE)
  357.         eprintf("failed to convert to mip\n");
  358.           __close(destHandle);
  359.         }
  360.         else
  361.           eprintf(failed_fileopen, outName);
  362.       }
  363.       /* convert to lump */
  364.       else if ((outType == TYPE_LUMP) && inPic) {
  365.         mprintf("write lump %s\n", outName);
  366.         if ((destHandle = __open(outName, H_WRITE_BINARY))) {
  367.           if (PutLMP(destHandle, inPic) == FALSE)
  368.         eprintf("failed to convert to lump\n");
  369.           __close(destHandle);
  370.         }
  371.         else
  372.           eprintf(failed_fileopen, outName);
  373.       }
  374.       /* convert to imagine */
  375.       else if (((outType == TYPE_IMAGINE) || (outType == TYPE_MAP)) && ((procType == TYPE_MAP) || (procType == TYPE_IMAGINE)) && inData) {
  376.         struct memory bspStatic;
  377.  
  378.         __memBase = &bspStatic;
  379.         bool retval = FALSE;
  380.  
  381.         mprintf("write to %s\n", outName);
  382.         if ((destFile = __fopen(outName, outType == TYPE_MAP ? "w" : F_WRITE_BINARY))) {
  383.           __bzero(bspMem, sizeof(struct memory));
  384.  
  385.           AllocClusters(bspMem, ALL_MAPS | LUMP_TEXINFO);
  386.  
  387.           if (procType == TYPE_MAP)
  388.         retval = LoadMapFile(bspMem, (char *)inData->rawdata);
  389.           else
  390.         retval = LoadTDDDFile(bspMem, inData->rawdata);
  391.  
  392.           if (retval) {
  393.         if (outType == TYPE_MAP)
  394.           retval = SaveMapFile(bspMem, destFile);
  395.         else
  396.           retval = SaveTDDDFile(bspMem, fileno(destFile));
  397.  
  398.         if (!retval)
  399.           eprintf(failed_filewrite, outName);
  400.           }
  401.           else
  402.         eprintf(failed_fileload, procName);
  403.  
  404.           FreeClusters(bspMem, 0);
  405.           __fclose(destFile);
  406.         }
  407.         else
  408.           eprintf(failed_fileopen, outName);
  409.       }
  410.       /*
  411.        * OP_ADD to pak-file 
  412.        */
  413.       else if (outType == TYPE_PACK) {
  414. /*        mprintf("%s %s to pak %s\n", OperationToText(procOper), procName, outName); */
  415.         if (!AddPAK(inPic, inData, outName, procOper))
  416.           eprintf("failed to add %s to pak %s\n", procName, outName);
  417.       }
  418.       /*
  419.        * OP_ADD to wad2-file 
  420.        */
  421.       else if (outType == TYPE_WAD2) {
  422.         if((arcType == TYPE_NONE) &&
  423.            ((procType == WAD2_CONPIC) || (procType == WAD2_MIPMAP) ||
  424.         (procType == WAD2_STATUSBAR) || (procType == WAD2_PALETTE)))
  425.           arcType = procType;
  426.       
  427. /*        mprintf("%s %s to wad %s\n", OperationToText(procOper), procName, outName); */
  428.         if (!AddWAD2(inPic, inData, outName, procOper, arcType))
  429.           eprintf("failed to add %s to wad %s\n", procName, outName);
  430.       }
  431.       /*
  432.        * OP_ADD to bsp-file 
  433.        */
  434.       else if (outType == TYPE_BSP) {
  435. /*        mprintf("%s %s to bsp %s\n", OperationToText(procOper), procName, outName); */
  436.         if (!AddBSP(inPic, inData, outName, procOper, arcType))
  437.           eprintf("failed to add %s to bsp %s\n", procName, outName);
  438.       }
  439.       /*
  440.        * OP_ADD to mdl-file 
  441.        */
  442.       else if (outType == TYPE_MODEL) {
  443.       }
  444.       /*
  445.        * OP_ADD to spr-file 
  446.        */
  447.       else if (outType == TYPE_SPRITE) {
  448.       }
  449.       /*
  450.        * view the pictures
  451.        */
  452.       else if ((procOper == OP_VIEW) && inPic) {
  453.         mprintf(oper_view, inPic->name, procName);
  454.         DisplayPicture(inPic->rawdata, inPic->name, inPic->width, inPic->height, 8, FALSE);
  455.       }
  456.       /*
  457.        * list the picture-data
  458.        */
  459.       else if ((procOper == OP_LIST) && inPic) {
  460.         mprintf(oper_view, inPic->name, procName);
  461.         mprintf(" width  %d\n height %d\n", inPic->width, inPic->height);
  462.       }
  463.       else
  464.         eprintf("dont know how to convert %s to %s\n", procName, outName);
  465.  
  466.       /*
  467.        * tfree resources 
  468.        */
  469.       if (inPic)
  470.         pfree(inPic);
  471.       if (inData)
  472.         rfree(inData);
  473.     }
  474.     else
  475.       eprintf(failed_fileread, procName);
  476.       }
  477.       else if (procType == TYPE_QUAKEC) {
  478.     char srcDir[NAMELEN_PATH], *tmp;
  479.  
  480.     __strncpy(srcDir, procName, NAMELEN_PATH - 1);
  481.     /* find parent dir */
  482.     if ((tmp = (char *)__strrchr(srcDir, '/')))
  483.       tmp[1] = '\0';
  484.     else
  485.       srcDir[0] = '\0';
  486.     if (!qcc(procFile, srcDir, procOper))
  487.       eprintf("cannot compile full %s\n", procName);
  488.       }
  489.       /* archives to archive */
  490.       else if ((outType == TYPE_PACK) && (procType != TYPE_PACK)) {
  491. /*    mprintf("%s %s to pak %s\n", OperationToText(procOper), procName, outName); */
  492.     if ((inData = GetRaw(fileno(procFile), arcName, 0))) {
  493.       if (!AddPAK(0, inData, outName, procOper))
  494.         eprintf("failed to %s %s to pak %s\n", OperationToText(procOper), procName, outName);
  495.       rfree(inData);
  496.     }
  497.     else
  498.       eprintf(failed_fileread, procName);
  499.       }
  500.       /* archive to parts */
  501.       else if ((procType == TYPE_PACK) ||
  502.            (procType == TYPE_WAD2) ||
  503.            (procType == TYPE_SPRITE) ||
  504.            (procType == TYPE_MODEL) ||
  505.            (procType == TYPE_CODE) ||
  506.            (procType == TYPE_BSP)) {
  507.     if (script) {
  508.       if ((scrpFile = __fopen(idxName, F_WRITE_BINARY))) {
  509.         fprintf(scrpFile, "process %s\n", procName);
  510.         if (dither)
  511.           fprintf(scrpFile, "dither %1d with %3d\n", 1, dithervalue);
  512.         else
  513.           fprintf(scrpFile, "dither %1d with %3d\n", 0, dithervalue);
  514.         if (Compression != CMP_NONE)
  515.           fprintf(scrpFile, "compress %1d with %3d\n", 1, Compression);
  516.         else
  517.           fprintf(scrpFile, "compress %1d with %3d\n", 0, Compression);
  518.       }
  519.     }
  520.  
  521.     if ((!destDir) || (*destDir == '\0')) {
  522.       ReplaceExt(idxName, "dir/");
  523.       destDir = idxName;
  524.     }
  525.  
  526.     /*
  527.      * pak to parts 
  528.      */
  529.     if (procType == TYPE_PACK) {
  530.       mprintf("%s %s from pak %s\n", OperationToText(procOper), arcName ? arcName : "everything", procName);
  531.       if (!ExtractPAK(fileno(procFile), scrpFile, destDir, arcName, outType, procOper, recurse))
  532.         eprintf("failed to extract full pak %s\n", procName);
  533.     }
  534.     /*
  535.      * wad to parts 
  536.      */
  537.     else if (procType == TYPE_WAD2) {
  538.       if((arcType == TYPE_NONE) &&
  539.          ((outType == WAD2_CONPIC) ||
  540.           (outType == WAD2_MIPMAP) ||
  541.           (outType == WAD2_STATUSBAR) ||
  542.           (outType == WAD2_PALETTE)))
  543.         arcType = outType;
  544.       
  545.       mprintf("%s %s from wad %s\n", OperationToText(procOper), arcName ? arcName : "everything", procName);
  546.       if (!ExtractWAD2(fileno(procFile), scrpFile, destDir, arcName, outType, procOper, arcType))
  547.         eprintf("failed to extract full wad %s\n", procName);
  548.     }
  549.     /*
  550.      * bsp to parts 
  551.      */
  552.     else if (procType == TYPE_BSP) {
  553.       mprintf("%s %s from bsp %s\n", OperationToText(procOper), arcName ? arcName : "everything", procName);
  554.       if (!ExtractBSP(fileno(procFile), scrpFile, destDir, arcName, outType, procOper, recurse))
  555.         eprintf("failed to extract full bsp %s\n", procName);
  556.     }
  557.     /*
  558.      * mdl to parts 
  559.      */
  560.     else if (procType == TYPE_MODEL) {
  561.     }
  562.     /*
  563.      * spr to parts 
  564.      */
  565.     else if (procType == TYPE_SPRITE) {
  566.     }
  567.     /*
  568.      * code to parts 
  569.      */
  570.     else if (procType == TYPE_CODE) {
  571.       if (!unqcc(fileno(procFile), destDir, procOper))
  572.         eprintf("cannot decompile full %s to %s\n", procName, destDir);
  573.     }
  574.  
  575.     if (scrpFile) {
  576.       fprintf(scrpFile, "finish %s\n", procName);
  577.       __fclose(scrpFile);
  578.       scrpFile = 0;
  579.     }
  580.       }
  581.       else
  582.     eprintf("dont know what to convert %s\n", procName);
  583.  
  584.     __fclose(procFile);
  585.   }
  586.   else
  587.     eprintf(failed_fileopen, procName);
  588.  
  589.   return TRUE;
  590. }
  591.  
  592. /*
  593.  * one of outName and outType MUST be given
  594.  */
  595. bool processName(char *procName, char *destDir, char *outName, filetype outType, char *arcName, filetype arcType,
  596.          operation procOper, bool script, bool recurse)
  597. {
  598.   char *procExt = GetExt(procName);
  599.   char *outExt = GetExt(outName);
  600.   filetype procType = TYPE_UNKNOWN;
  601.   char newName[NAMELEN_PATH + 1];
  602.   struct stat procStat;
  603.  
  604.   /*
  605.    * if the dirname doesn't end with / we must add it, hmpf
  606.    */
  607.   if (destDir) {
  608.     int len = __strlen(destDir);
  609.  
  610.     if (!((destDir[len - 1] == '/') || (destDir[len - 1] == ':') || (destDir[len - 1] == '\0'))) {
  611.       char *newDir;
  612.  
  613.       if ((newDir = (char *)tmalloc(len + 2))) {
  614.     __strcpy(newDir, destDir);
  615.     newDir[len] = '/';
  616.     destDir = newDir;
  617.       }
  618.     }
  619.   }
  620.   else
  621.     destDir = "\0";
  622.  
  623.   if(stat(procName, &procStat) < 0) {
  624.     eprintf("file or dir %s does not exists\n", procName);
  625.     return FALSE;
  626.   }
  627.  
  628.   if(!S_ISDIR(procStat.st_mode)) {
  629.     /*
  630.      * set default proctype
  631.      */
  632.          if (!__strcmp(procExt, "pal")) { procType = TYPE_PALETTE; }
  633.     else if (!__strcmp(procExt, "bsp")) { procType = TYPE_BSP; }
  634.     else if (!__strcmp(procExt, "cfg")) { procType = TYPE_CONFIG; }
  635.     else if (!__strcmp(procExt, "dat")) { procType = TYPE_CODE; }
  636.     else if (!__strcmp(procExt, "dem")) { procType = TYPE_DEMO; }
  637.  /* else if (!__strcmp(procExt, "dir")) { procType = TYPE_DIRECTORY; } */
  638.     else if (!__strcmp(procExt, "frm")) { procType = TYPE_LUMP; }
  639.     else if (!__strcmp(procExt, "idx")) { procType = TYPE_INDEX; }
  640.     else if (!__strcmp(procExt, "iff")) { procType = TYPE_IMAGE; }
  641.     else if (!__strcmp(procExt, "iob")) { procType = TYPE_IMAGINE; }
  642.     else if (!__strcmp(procExt, "jpg")) { procType = TYPE_IMAGE; }
  643.     else if (!__strcmp(procExt, "lbm")) { procType = TYPE_IMAGE; }
  644.     else if (!__strcmp(procExt, "lit")) { procType = TYPE_LIT; }
  645.     else if (!__strcmp(procExt, "lmp")) { procType = TYPE_LUMP; }
  646.     else if (!__strcmp(procExt, "map")) { procType = TYPE_MAP; }
  647.     else if (!__strcmp(procExt, "mdl")) { procType = TYPE_MODEL; }
  648.     else if (!__strcmp(procExt, "mip")) { procType = TYPE_MIPMAP; }
  649.     else if (!__strcmp(procExt, "pak")) { procType = TYPE_PACK; }
  650.     else if (!__strcmp(procExt, "pgm")) { procType = TYPE_IMAGE; }
  651.     else if (!__strcmp(procExt, "png")) { procType = TYPE_IMAGE; }
  652.     else if (!__strcmp(procExt, "ppm")) { procType = TYPE_IMAGE; }
  653.     else if (!__strcmp(procExt, "pnm")) { procType = TYPE_IMAGE; }
  654.     else if (!__strcmp(procExt, "prt")) { procType = TYPE_PRT; }
  655.     else if (!__strcmp(procExt, "rc" )) { procType = TYPE_RESOURCE; }
  656.     else if (!__strcmp(procExt, "skn")) { procType = TYPE_LUMP; }
  657.     else if (!__strcmp(procExt, "spr")) { procType = TYPE_SPRITE; }
  658.     else if (!__strcmp(procExt, "src")) { procType = TYPE_QUAKEC; }
  659.     else if (!__strcmp(procExt, "stb")) { procType = TYPE_STATUSBAR; }
  660.     else if (!__strcmp(procExt, "tri")) { procType = TYPE_TRIANGLE; }
  661.     else if (!__strcmp(procExt, "vis")) { procType = TYPE_VIS; }
  662.     else if (!__strcmp(procExt, "wad")) { procType = TYPE_WAD2; }
  663.     else if (!__strcmp(procExt, "wal")) { procType = TYPE_WAL; }
  664.     else if (!__strcmp(procExt, "wav")) { procType = TYPE_WAVE; }
  665.   }
  666.   else {
  667.     if(!recurse) {
  668.       mprintf("%s is a directory, use -r/--recurse\n", procName);
  669.       return TRUE;
  670.     }
  671.     procType = TYPE_DIRECTORY;
  672.   }
  673.  
  674.   /*
  675.    * set default outtype
  676.    */
  677.   if (outName) {
  678.     if (outType == TYPE_NONE) {
  679.        if (!__strcmp(outExt, "pal")) { outType = TYPE_PALETTE; }
  680.       else if (!__strcmp(outExt, "bsp")) { outType = TYPE_BSP; }
  681.       else if (!__strcmp(outExt, "cfg")) { outType = TYPE_CONFIG; }
  682.       else if (!__strcmp(outExt, "dat")) { outType = TYPE_CODE; }
  683.       else if (!__strcmp(outExt, "dem")) { outType = TYPE_DEMO; }
  684.       else if (!__strcmp(outExt, "dir")) { outType = TYPE_DIRECTORY; }
  685.       else if (!__strcmp(outExt, "frm")) { outType = TYPE_FRAME; }
  686.       else if (!__strcmp(outExt, "idx")) { outType = TYPE_INDEX; }
  687.       else if (!__strcmp(outExt, "iff")) { outType = TYPE_ILBM; }
  688.       else if (!__strcmp(outExt, "iob")) { outType = TYPE_IMAGINE; }
  689.       else if (!__strcmp(outExt, "jpg")) { outType = TYPE_JPEG; }
  690.       else if (!__strcmp(outExt, "lbm")) { outType = TYPE_ILBM; }
  691.       else if (!__strcmp(outExt, "lit")) { outType = TYPE_LIT; }
  692.       else if (!__strcmp(outExt, "lmp")) { outType = TYPE_LUMP; }
  693.       else if (!__strcmp(outExt, "map")) { outType = TYPE_MAP; }
  694.       else if (!__strcmp(outExt, "mdl")) { outType = TYPE_MODEL; }
  695.       else if (!__strcmp(outExt, "mip")) { outType = TYPE_MIPMAP; }
  696.       else if (!__strcmp(outExt, "pak")) { outType = TYPE_PACK; }
  697.       else if (!__strcmp(outExt, "png")) { outType = TYPE_PNG; }
  698.       else if (!__strcmp(outExt, "ppm")) { outType = TYPE_PPM; }
  699.       else if (!__strcmp(outExt, "pnm")) { outType = TYPE_PPM; }
  700.       else if (!__strcmp(outExt, "prt")) { outType = TYPE_PRT; }
  701.       else if (!__strcmp(outExt, "rc" )) { outType = TYPE_RESOURCE; }
  702.       else if (!__strcmp(outExt, "skn")) { outType = TYPE_SKIN; }
  703.       else if (!__strcmp(outExt, "spr")) { outType = TYPE_SPRITE; }
  704.       else if (!__strcmp(outExt, "src")) { outType = TYPE_QUAKEC; }
  705.       else if (!__strcmp(outExt, "stb")) { outType = TYPE_STATUSBAR; }
  706.       else if (!__strcmp(outExt, "tri")) { outType = TYPE_TRIANGLE; }
  707.       else if (!__strcmp(outExt, "vis")) { outType = TYPE_VIS; }
  708.       else if (!__strcmp(outExt, "wad")) { outType = TYPE_WAD2; }
  709.       else if (!__strcmp(outExt, "wal")) { outType = TYPE_WAL; }
  710.       else if (!__strcmp(outExt, "wav")) { outType = TYPE_WAVE; }
  711.     }
  712.     __strncpy(newName, outName, NAMELEN_PATH - 1);
  713.   }
  714.   /*
  715.    * set default outname
  716.    */
  717.   else {
  718.     __strncpy(newName, destDir, NAMELEN_PATH - 1);
  719.     __strncat(newName, procName, NAMELEN_PATH - 1);
  720.     switch (outType) {
  721.       case TYPE_PPM:    ReplaceExt(newName, "ppm"); break;
  722.       case TYPE_JPEG:    ReplaceExt(newName, "jpg"); break;
  723.       case TYPE_ILBM:    ReplaceExt(newName, "iff"); break;
  724.       case TYPE_PNG:    ReplaceExt(newName, "png"); break;
  725.       case TYPE_WAL:    ReplaceExt(newName, "wal"); break;
  726.       case TYPE_MIPMAP:    ReplaceExt(newName, "mip"); break;
  727.       case TYPE_LUMP:    ReplaceExt(newName, "lmp"); break;
  728.       default:                        break;
  729.     }
  730.   }
  731.  
  732.   /*
  733.    * set default arcname
  734.    *
  735.    * behaviour:
  736.    *  * / *.* -> *.pak  arcName = * / *.*
  737.    *  * / *.* -> *.wad  arcName =     *
  738.    *  * / *.* -> *.bsp  arcName =     *
  739.    *  *.pak -> * / *.*  arcName = * / *.*
  740.    *  *.wad -> * / *.*  arcName =     *
  741.    *  *.bsp -> * / *.*  arcName =     *
  742.    *  *.bsp -> *.wad    arcName = *.wad
  743.    */
  744.   if (!arcName) {
  745.     if ((outType == TYPE_PACK) ||
  746.     ((outType == TYPE_WAD2) && (procType != TYPE_WAD2)) ||
  747.     (outType == TYPE_SPRITE) ||
  748.     (outType == TYPE_MODEL) ||
  749.     (outType == TYPE_BSP)) {
  750.       arcName = smalloc(procName);
  751.       if (arcType == TYPE_NONE)
  752.     arcType = procType;
  753.     }
  754.     else {
  755.       arcName = smalloc(outName);
  756.       if (arcType == TYPE_NONE)
  757.     arcType = outType;
  758.     }
  759.   }
  760.   if (!(((outType == TYPE_PACK) || (procType == TYPE_PACK)) ||
  761.     (((outType == TYPE_MAP) || (outType == TYPE_IMAGINE) || (outType == TYPE_LIT) || (outType == TYPE_VIS) || (outType == TYPE_WAD2)) && (procType == TYPE_BSP)) ||
  762.     ((outType == TYPE_WAD2) && (procType == TYPE_WAD2)))) {
  763.     arcName = GetFile(arcName);
  764.     StripExt(arcName);
  765.   }
  766.  
  767.   outName = newName;
  768.   return processType(procName, procType, destDir, outName, outType, arcName, arcType, procOper, script, recurse);
  769. }
  770.